// 7-1 顺序存储的二叉树的最近的公共祖先问题
#include "..\headers\ListOf.h"

// Find their nearest common ancestor
void find_nearest_common_ancestor(int binarytree[], int i, int j) {
  // Find the depth of i and j
  int depth_i = log2(i) + 1, depth_j = log2(j) + 1;
  // Find the ancestor of i and j
  while (i != j) {
    if (depth_i > depth_j) {
      i = i / 2;
      depth_i--;
    } else if (depth_j > depth_i) {
      j = j / 2;
      depth_j--;
    } else {
      i = i / 2;
      j = j / 2;
      depth_i--;
      depth_j--;
    }
  }
  printf("%d %d", i, binarytree[i]);
  return;
}

int main() {
  int binarytree[1001] = {0};
  int n;
  // Take input from user
  scanf("%d", &n);
  if (n == 0) {
    return 0;
  }

  // Iterate through the array and input the values
  for (int i = 1; i <= n; i++) scanf("%d", &binarytree[i]);
  int i = 0, j = 0;
  // Take input from user i and j is number of the nodes
  scanf("%d %d", &i, &j);
  if (binarytree[i] == 0 || binarytree[j] == 0) {
    if (binarytree[i] == 0)
      printf("ERROR: T[%d] is NULL", i);
    else
      printf("ERROR: T[%d] is NULL", j);
    return 0;
  }

  // Find their nearest common ancestor
  find_nearest_common_ancestor(binarytree, i, j);

  return 0;
}